# 设置编译标准（保持不变）
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 17)

# 以下保持注释状态
#add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-finlines-visible>)
#add_compile_options(-fvisibility=hidden)

# 设置全局编译器变量（使用相对路径或环境变量），确保添加子目录时不会因为编译器未设置而报错
set(MY_C_COMPILER ${CMAKE_C_COMPILER} CACHE FILEPATH "C compiler" FORCE)
set(MY_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE FILEPATH "CXX compiler" FORCE)
set(MY_ASM_COMPILER ${CMAKE_C_COMPILER} CACHE FILEPATH "ASM compiler" FORCE)

# 设置编译器选项（使用add_compile_options）
add_compile_options(
        -ffile-prefix-map=${CMAKE_SOURCE_DIR}=.
        $<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>
        $<$<COMPILE_LANGUAGE:ASM>:-D_GLIBCXX_ASSERTIONS>
        $<$<COMPILE_LANGUAGE:ASM>:-fstack-clash-protection>
        $<$<COMPILE_LANGUAGE:ASM>:-fstack-protector-strong>
)

# 设置可执行文件后缀
set(CMAKE_EXECUTABLE_SUFFIX_ASM     ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_C       ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_CXX     ".elf")

# 设置编译模式
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# 设置编译器路径（使用add_compile_options处理环境变量）
add_compile_options($<$<COMPILE_LANGUAGE:C>:-fno-omit-frame-pointer>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-omit-frame-pointer>)

#--------------------------编译优化----------------------------
# 根据构建类型设置优化级别
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    add_compile_options(-O0 -g3)
    message(STATUS "Debug模式: 使用 -Og -g3 优化选项")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
    add_compile_options(-O2)
    message(STATUS "Release模式: 使用 -O2 优化选项")
else()
    # 默认使用Debug配置
    set(CMAKE_BUILD_TYPE Debug CACHE STRING "构建类型" FORCE)
    add_compile_options(-Og -g3)
    message(STATUS "未指定构建类型，默认使用Debug模式: -Og -g3")
endif()


#-------------------------- MCU ----------------------------
add_compile_options(
        -mcpu=cortex-m4
        -mfpu=fpv4-sp-d16
        -mfloat-abi=hard
        -mthumb
        -mthumb-interwork
)
add_link_options(
        -mcpu=cortex-m4
        -mfpu=fpv4-sp-d16
        -mfloat-abi=hard
        -mthumb
        -mthumb-interwork
)

# ====================================== 编译选项 ======================================
# --------------- ASM ------------------
add_compile_options(
        $<$<COMPILE_LANGUAGE:ASM>:-x>
        $<$<COMPILE_LANGUAGE:ASM>:assembler-with-cpp>
        $<$<COMPILE_LANGUAGE:ASM>:-MMD>
        $<$<COMPILE_LANGUAGE:ASM>:-MP>
)

# --------------- C ------------------
add_compile_options(
        -Wextra
        -Wpedantic
        -fdata-sections
        -ffunction-sections
        -fno-common
        -fmessage-length=0
        -finline-functions
        -Winline
)

# --------------- C++ ------------------
add_compile_options(
        $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
        $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
        $<$<COMPILE_LANGUAGE:CXX>:-fno-threadsafe-statics>
)

# ====================================== 链接选项 ======================================
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Firmware/linker/STM32F407VETX_FLASH.ld)
add_link_options(
        -T${LINKER_SCRIPT}
#        --specs=nano.specs
        -Wl,--gc-sections
        -Wl,--relax
        -Wl,--as-needed
        -Wl,--print-memory-usage
        -Wl,-Map=${CMAKE_PROJECT_NAME}.map
        -Wl,--start-group
        -lc
        -lm
        -Wl,--end-group
        $<$<LINK_LANGUAGE:CXX>:-lstdc++>
        $<$<LINK_LANGUAGE:CXX>:-lsupc++>
        -Wl,-z,noexecstack
)

# ====================================== 禁用特定警告 ======================================
add_compile_options(-Wno-missing-field-initializers)

# 宏定义
add_compile_options(
        -DARM_MATH_CM4
        -DARM_MATH_MATRIX_CHECK
        -DARM_MATH_ROUNDING
        -DDEBUG
        -DUSE_HAL_DRIVER
        -DUSE_FULL_LL_DRIVER
        -DSTM32F407xx
)